package org.lq.er.view.student_info;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.lq.er.entity.Student;
import org.lq.er.service.StudentService;
import org.lq.er.service.impl.StudentServiceImpl;

import java.awt.FlowLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
 * 修改学生信息视图
 * @author 宋铀
 * 
 * @package org.lq.er.view.student_info
 * @date 2020年8月24日下午8:15:04
 */
public class ER_student_update extends JFrame {

	private JPanel contentPane;
	private JTextField text_cardId;
	private JTextField text_name;
	private JTextField text_passwd;
	private JTextField text_phoneNo;
	private JTextField text_classes;
	private JTextField text_stuNo;
	private static Student student = null;
	private StudentService studentService = new StudentServiceImpl();
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ER_student_update frame = new ER_student_update(student);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ER_student_update(Student student) {
		setTitle("修改学生信息");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 314, 388);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBackground(Color.white);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		
		JLabel lbl_cardId = new JLabel("身份证号:");
		lbl_cardId.setBounds(54, 46, 54, 15);
		
		JLabel lbl_name = new JLabel("姓名:");
		lbl_name.setBounds(78, 85, 30, 15);
		
		JLabel lbl_passwd = new JLabel("密码:");
		lbl_passwd.setBounds(78, 124, 30, 15);
		
		JLabel lbl_phoneNo = new JLabel("手机号码:");
		lbl_phoneNo.setBounds(54, 163, 54, 15);
		
		JLabel lbl_classes = new JLabel("班级:");
		lbl_classes.setBounds(78, 202, 30, 15);
		
		JLabel lbl_stuNo = new JLabel("学号:");
		lbl_stuNo.setBounds(78, 241, 30, 15);
		
		text_cardId = new JTextField();
		text_cardId.setEditable(false);
		text_cardId.setBounds(118, 43, 136, 21);
		text_cardId.setColumns(10);
		
		text_name = new JTextField();
		text_name.setEditable(false);
		text_name.setBounds(118, 82, 136, 21);
		text_name.setColumns(10);
		
		text_passwd = new JTextField();
		text_passwd.setEditable(false);
		text_passwd.setBounds(118, 121, 85, 21);
		text_passwd.setColumns(10);
		
		text_phoneNo = new JTextField();
		text_phoneNo.setBounds(118, 160, 136, 21);
		text_phoneNo.setColumns(10);
		
		text_classes = new JTextField();
		text_classes.setBounds(118, 199, 136, 21);
		text_classes.setColumns(10);
		
		text_stuNo = new JTextField();
		text_stuNo.setBounds(118, 238, 136, 21);
		text_stuNo.setColumns(10);
		panel.setLayout(null);
		panel.add(lbl_name);
		panel.add(lbl_cardId);
		panel.add(lbl_passwd);
		panel.add(lbl_phoneNo);
		panel.add(lbl_classes);
		panel.add(lbl_stuNo);
		panel.add(text_classes);
		panel.add(text_phoneNo);
		panel.add(text_name);
		panel.add(text_passwd);
		panel.add(text_cardId);
		panel.add(text_stuNo);
		
		JButton btn_save = new JButton("保存");
		btn_save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 创建学生对象
				Student student = new Student();
				student.setCard_id(text_cardId.getText());
				student.setName(text_name.getText());
				student.setPassword(text_passwd.getText());
				student.setPhone_no(text_phoneNo.getText());
				student.setClasses(text_classes.getText());
				int stuNo = Integer.parseInt(text_stuNo.getText());
				student.setStu_no(stuNo);
				System.out.println(student.toString());
				// 询问是否修改
				int num = JOptionPane.showConfirmDialog(ER_student_update.this, "是否修改?", "提示", JOptionPane.INFORMATION_MESSAGE);
				if (num == 0) {
					// 删除操作
					if (studentService.update(student)) {
						JOptionPane.showMessageDialog(ER_student_update.this, "修改成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
						dispose();
					} else {
						JOptionPane.showMessageDialog(ER_student_update.this, "修改失败!", "错误", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		});
		btn_save.setBounds(95, 283, 93, 23);
		panel.add(btn_save);
		
		JRadioButton rdbtn_restPasswd = new JRadioButton("初始密码");
		rdbtn_restPasswd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (rdbtn_restPasswd.isSelected()) {
					text_passwd.setText("123456");
				}
			}
		});
		rdbtn_restPasswd.setBounds(209, 120, 73, 23);
		panel.add(rdbtn_restPasswd);
		
		initText(student);
	}
	
	/**
	 * 初始化文本内容
	 * @param student
	 */
	private void initText(Student student) {
		text_cardId.setText(student.getCard_id());
		text_name.setText(student.getName());
		text_passwd.setText(student.getPassword());
		text_phoneNo.setText(student.getPhone_no());
		text_classes.setText(student.getClasses());
		text_stuNo.setText(student.getStu_no()+"");
	}
}
